<?
include("class/Member.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID=$_SESSION['sid'];
$m=new Member($s,$memberID);
$m_info=$m->info();
$statusT=array("0"=>"<em class='red'>未结算</em>",1=>"<em class='orange'>结算中</em>",2=>"<em class='green'>已结算</em>");
/*自动结算配置******************/

/*分销商结算配置*/
$fids=$m->distributors();					//分销商
$settlefids=$m->settlefids();				//已配置分销商
$unsettlefids=array_diff($fids,$settlefids);//未配置分销商
$unfids=array_diff($settlefids,$fids);      //解除分销关系的分销商
//已解除分销关系商家，停用结算配置
if(count($unfids)!=0){
	$unfids=implode(",",$unfids);
	$str="update uu_settle set status=1 where fid in ($unfids) and aid=$memberID";
	$GLOBALS['le']->query($str);
}
//未配置分销商，自动插入配置，按月结
if(count($unsettlefids)!=0){
	foreach($unsettlefids as $fid){
		$str="select creattime from pft_member where id=$fid";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$creattime=$GLOBALS['le']->f('creattime');
		$str="select id from uu_settle where fid=$fid and aid=$memberID limit 1";
		$GLOBALS['le']->query($str);
		if(!$GLOBALS['le']->fetch_assoc()){
			$str="insert uu_settle set fid=$fid,aid=$memberID,billdate=31,begintime='$creattime'";
			$GLOBALS['le']->query($str);
		}
	}
}
//分销关系的分销商，结算配置需有效
if(count($fids)!=0){					
	$str="update uu_settle set status=0 where fid in (".implode(",",$fids).") and aid=$memberID";
	$GLOBALS['le']->query($str);
}



/*供应商结算配置*/
$aids=$m->appiers();							//供应商
$settleaids=$m->settleaids();					//已配置供应商
$unsettleaids=array_diff($aids,$settleaids);	//未配置供应商
$unaids=array_diff($settleaids,$aids);			//解除分销关系的分销商
//已解除供销关系商家，停用结算配置
if(count($unaids)!=0){
	$unaids=implode(",",$unaids);
	$str="update uu_settle set status=1 where aid in ($unaids) and fid=$memberID";
	$GLOBALS['le']->query($str);
}
//未配置分销商，自动插入配置，按月结
if(count($unsettleaids)!=0){
	$creattime=$m_info['creattime'];
	foreach($unsettleaids as $aid){
		$str="select id from uu_settle where aid=$aid and fid=$memberID limit 1";
		$GLOBALS['le']->query($str);
		if(!$GLOBALS['le']->fetch_assoc()){
			$str="insert uu_settle set aid=$aid,fid=$memberID,billdate=31,begintime='$creattime'";
			$GLOBALS['le']->query($str);
		}
	}
}
//供销关系的供应商，结算配置需有效
if(count($aids)!=0){
	$str="update uu_settle set status=0 where aid in (".implode(",",$aids).") and fid=$memberID";
	$GLOBALS['le']->query($str);
}
/*结算配置*end*****************/


include("class/Bill.php");
/*按结算配置自动生成应收款账单*/
$gets=array();
$str="select fid from uu_settle where aid=$memberID and status=0";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
	$gets[]=$row['fid'];
}
foreach($gets as $id){
	$b=new Bill($id,$memberID,$s,$GLOBALS['le']);
	$b->getbill();
}
/*按结算配置自动生成应付款账单*/
$pays=array();
$str="select * from uu_settle where fid=$memberID and status=0";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
	$pays[]=$row;
}
foreach($gets as $id){
	$b=new Bill($memberID,$id,$s,$GLOBALS['le']);
	$b->getbill();
}


$str="select id,dname from pft_member";
$dnames=array();
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
	$dnames[$row['id']]=$row['dname'];
}

?>